import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp();
var url = app.data.url;
var map = {};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    id: '',
    merchantId: ''
  },

  /**
   * 获取money
   */
  onChange(event) {
    // event.detail 为当前输入的值
    let that = this;
    that.setData({
      money: event.detail
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _that = this;
    let merchantId = options.merchantId;
    _that.setData({
      merchantId: merchantId
    });
    wx.request({
      url: url + '/ItemSendPriseApi/querySendPriseByMerchantId',
      data: {
        id: merchantId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 200) {
          let item = res.data.data;
          _that.setData({
            money: item.price,
            id: item.id
          })
        } 
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },

  /**
   * 新增
   */
  saveOrUpdate: function(){
    let _that = this;
    let merchantId = _that.data.merchantId;
    let money = _that.data.money;
    let id = _that.data.id;
    if(id){
      // 编辑
      wx.request({
        url: url + '/ItemSendPriseApi/editSendPrise',
        data: {
          id: id,
          merchantId: merchantId,
          price: money
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == 200) {
            console.log(res.data.data);
            wx.showToast({
              title: '编辑成功!',
              icon: "none",
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '操作失败!',
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.info('submit fail');
        },
        complete: function (res) {
          console.info('submit complete');
        }
      })
    } else {
      // 新增
      wx.request({
        url: url + '/ItemSendPriseApi/insertSendPrise',
        data: {
          merchantId: merchantId,
          price: money
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 200) {
            console.log(res.data.data);
            wx.showToast({
              title: '新增成功!',
              icon: "none",
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '操作失败!',
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.info('submit fail');
        },
        complete: function (res) {
          console.info('submit complete');
        }
      })
    }

   
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})